package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.dto.BlogAEditDto;
import cn.cheegon.blog.entity.dto.CmsBlogDto;
import cn.cheegon.blog.entity.req.BlogAddReq;
import cn.cheegon.blog.entity.req.BlogEditReq;
import cn.cheegon.blog.entity.req.BlogTagReq;
import cn.cheegon.blog.entity.rsp.BlogListRsp;
import cn.cheegon.blog.model.CmsBlog;
import cn.cheegon.blog.service.CmsBlogService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/admin/blog/*")
public class AdminBlogController extends AdminController{

    @Autowired
    private CmsBlogService cmsBlogService;

    @GetMapping("list")
    public CommonResult<CommonPage<BlogListRsp>> getBlogList(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize,
                                                             @RequestParam(value = "kwd", required = false) String kwd,
                                                             @RequestParam(value = "tagId", required = false) Long tagId,
                                                             @RequestParam(value = "typeId", required = false) Long typeId) {
        return CommonResult.success(cmsBlogService.getList(kwd, tagId, typeId, pageNum, pageSize));
    }

    @GetMapping("recommendList")
    public CommonResult<List<BlogListRsp>> getRecommendBlogList(@RequestParam(name = "size", defaultValue = "6") Integer size) {
        return CommonResult.success(cmsBlogService.getRecommendList(size));
    }

    @GetMapping("detail/{id}")
    public CommonResult<CmsBlogDto> getBlog(@PathVariable Long id) {
        return CommonResult.success(cmsBlogService.getItem(id));
    }



    @PostMapping("add")
    public CommonResult add(@RequestBody BlogAddReq req) {
        checkAdmin();
        BlogAEditDto bean = BeanUtil.toBean(req, BlogAEditDto.class);
        bean.setTagIds(req.getTagIds());
        cmsBlogService.add(bean);
        return CommonResult.success();
    }
    @PostMapping("edit")
    public CommonResult edit(@RequestBody BlogEditReq req) {
        checkAdmin();
        BlogAEditDto bean = BeanUtil.toBean(req, BlogAEditDto.class);
        bean.setTagIds(req.getTagIds());
        cmsBlogService.edit(bean);
        return CommonResult.success();
    }
    @GetMapping("del/{id}")
    public CommonResult del(@PathVariable Long id) {
        checkAdmin();
        cmsBlogService.del(id);
        return CommonResult.success();
    }
    @PostMapping("addTag")
    public CommonResult addTag(@RequestBody BlogTagReq req) {
        checkAdmin();
        cmsBlogService.addTag(req);
        return CommonResult.success();
    }

    @PostMapping("delTag")
    public CommonResult delTag(@RequestBody BlogTagReq req) {
        checkAdmin();
        cmsBlogService.delTag(req);
        return CommonResult.success();
    }
}
